package com.example.demo.util;


import com.alibaba.fastjson.JSON;

/**
 * Json序列化反序列化单元
 * eg.
 * 	String ucStr = JsonUtil.toJSONString(uc);
 * 	System.out.println(ucStr);
 * 	
 * 	UserCache uc2 = (UserCache) JsonUtil.parseObject(ucStr, UserCache.class);
 * 	System.out.println(uc2.getDb_area());
 * 	
 * @author 徐晓华
 * **/
public class JsonUtil {
	
	/**
	 * 对象序列化成json字符串
	 * **/
	@SuppressWarnings("finally")
	public static String toJSONString(Object object) {
		String result = null;
		try {
			result = JSON.toJSONString(object);
		}catch(Exception e) {
			
		}finally {
			return result;
		}
	}

	/**
	 * json字符串反序列化成对象
	 * **/
	@SuppressWarnings("finally")
	public static <T> Object parseObject(String text, Class<T> clazz) {
		Object result = null;
		try {
			result = JSON.parseObject(text);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			return result;
		}
	}

	/**
	 * json字符串反序列化成对象
	 * **/
	@SuppressWarnings("finally")
	public static <T> Object parseArray(String text, Class<T> clazz) {
		Object result = null;
		try {
			result = JSON.parseArray(text);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			return result;
		}
	}
}
